<template>
	<view>
		<cu-custom bgColor="bg-gradual-green" :isBack="true">
			<block slot="backText"></block>
			<block slot="content">捐赠</block>
		</cu-custom>
		<view class="cu-bar bg-white solid-bottom">
			<view class="action">
				<text class='cuIcon-title text-red'></text>
				资金直接转到求助人微信账号,不收取任何手续费
			</view>
		</view>
		<view class="padding">
			<image :src="ewmImg" mode="widthFix" style="margin: 0 auto;display: block;width:100%;"></image>
		</view>
		
		<view class="padding">
			<button v-if="openSettingBtnHidden" class="cu-btn bg-gradual-green lg block" @click="saveEwm">
				保存到相册
			</button>
			<button v-else class="cu-btn bg-gradual-green lg block" hover-class="none" open-type="openSetting" @opensetting='handleSetting'>保存到相册</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				openSettingBtnHidden: true, //是否授权
				ewmImg: '' //这是要保存的图片
			}
		},
		onShow() {

		},
		methods: {
			handleSetting(e) {
				if (!e.detail.authSetting['scope.writePhotosAlbum']) {
					this.openSettingBtnHidden = false;
				} else {
					this.openSettingBtnHidden = true;
				}
			},
			saveEwm: function(e) {
				let _self = this;
				//获取相册授权
				uni.getSetting({
					success(res) {
						if (!res.authSetting['scope.writePhotosAlbum']) {
							uni.authorize({
								scope: 'scope.writePhotosAlbum',
								success() {
									//这里是用户同意授权后的回调
									_self.saveImgToLocal();
								},
								fail() { //这里是用户拒绝授权后的回调
									_self.openSettingBtnHidden = false
								}
							})
						} else { //用户已经授权过了
							_self.saveImgToLocal();
						}
					}
				})
			},
			saveImgToLocal: function(e) {
				let _self = this;
				uni.showModal({
					title: '提示',
					content: '确定保存到相册吗',
					success: function(res) {
						if (res.confirm) {

							uni.downloadFile({
								url: _self.ewmImg, //图片地址
								success: (res) => {
									console.log(res)
									if (res.statusCode === 200) {
										uni.saveImageToPhotosAlbum({
											filePath: res.tempFilePath,
											success: function() {
												uni.showToast({
													title: "保存成功",
												});
											},
											fail: function() {
												uni.showToast({
													title: "保存失败",
													icon: "none"
												});
											}
										});
									}
								},
								fail: (err) => {
									console.log(err)
								}

							})


						} else if (res.cancel) {

						}
					}
				});
			}
		},
		onHide() {},
		onLoad(option) {
			console.log(option)
			this.ewmImg = option.url;
		},
		computed: {},
		onReachBottom() {},
	}
</script>
